import {
    getDispatchList
} from '../../api/long-insure'
import {
    ocrIdCard,
    batchIdCardNoVerify
} from '../../../api/app'
import {
    wxfileTobase64,
    getCardAge
} from '../../../utils/util'
import {
    checkperiodareaApi
} from '../../api/long-insure'
const app = getApp()
Page({
    data: {
        planId: 0,
        specialAppoint: '',
        name: '',
        idcard: '',
        dispatchList: null,
        showDispatch: false,
        dispatch: '',
        workType: '',
        workTypes: [],
        type: '',
        back: '',
        insur: '',
        planCode: ''
    },
    changespecialAppointstatus(e) {
        console.log(e)
        let i = e.currentTarget.dataset.i * 1
        if (i === 1) {
            this.setData({
                isselectspecialAppoint: false
            })
        } else {
            this.setData({
                isselectspecialAppoint: true
            })
        }
    },
    // type=add&back=manual&insur=period_001_zgrs
    async onLoad(options) {
        console.log(options, 'options')
        this.setData({
            planId: options.planId,
            type: options.type,
            back: options.back,
            insur: options.insur,
            planCode: options.plancode
        })
    },
    async onDispatch() {
        this.setData({
            workTypes: []
        })
        const result = await getDispatchList({
            pageNum: 1,
            pageSize: 100,
            status: 1,
            insurCompany: this.data.insur,
            planCode: this.data.planCode,
            isGetArea: 1
        })

        if (!result.list?.length) {
            wx.showModal({
                title: '提示',
                content: '无审核通过的”被派遣单位”\r\n\r\n解决方法:请在"审核派遣单位页"审核“派遣单位"',
                showCancel: false
            })
        }

        this.setData({
            dispatchList: result.list,
            showDispatch: true,
            workTypes: false
        })
    },
    async onDispatchEnter(event) {
        let res = await checkperiodareaApi({
            planId: this.data.planId
        })
        if (event.currentTarget.dataset.isdisabled) {
            wx.showToast({
                title: '已禁用',
                icon: 'none'
            })
            return
        }
        if (!res.isWhitelist && event.currentTarget.dataset.area === '天津' && this.data.insur === 'period_006_gyas') {
            wx.showToast({
                title: '抱歉，当前方案不支持该（天津）地区投保',
                icon: 'none'
            })
            return
        }
        const index = event.currentTarget.dataset.index
        // const text  = event.currentTarget.dataset.text
        const text = '1、仅承保视频中的对应风险等级工种，其它工种请再提供视频确认2、超风险投保警示:严格按照实际工种对应风险等级进行投保，严禁超风险投保，否则保险公司有权拒赔。如将4类及以上工种投保至1-3类工种属于超风险投保工种'
        const dispatch = this.data.dispatchList[index]
        if (!dispatch.occupationList.length) {
            wx.showModal({
                title: '提示',
                content: '很抱歉，您当前所选择派遣单位的可用工种，不在该方案的承保范围内，请联系您的专属客户经理。',
                showCancel: false
            })
        }
        this.setData({
            dispatch: dispatch,
            dispatchList: [],
            workType: '',
            specialAppoint: text,
            showDispatch: false,
        })
        // this.setData({
        //     dispatchList: result.list
        // })
    },
    onWorkType() {
        this.setData({
            workTypes: this.data.dispatch.occupationList
        })
    },
    onWorkTypeEnter(event) {
        const index = event.currentTarget.dataset.index
        this.setData({
            workTypes: [],
            workType: this.data.workTypes[index]
        })
    },
    onGoDispatch() {
        wx.navigateTo({
            url: '/packageLongInsure/pages/dispatch/dispatch',
        })
    },
    async onSubmit() {
        if (!/^\d{17}[\dXx]$/.test(this.data.idcard)) {
            app.toast('请输入正确的员工身份证')
            return
        }
        if (this.data.specialAppoint && !this.data.isselectspecialAppoint) {
            app.toast('请勾选特别约定')
            return
        }
        this.setData({
            idcard: this.data.idcard.replace(/x/g, 'X')
        })
        const age = getCardAge(this.data.idcard)
        console.log(age, 'this.data.idcard')
        if (age < 16) {
            app.toast('年龄小于16不可投保')
            return
        }
        if (this.data.type === 'add') {
            if (!this.data.dispatch) {
                app.toast('请选择被派遣单位')
                return
            }
            if (!this.data.workType) {
                app.toast('请选择工种')
                return
            }
        }
        if (!this.data.name) {
            app.toast('请输入员工姓名')
            return
        }
        const result = await batchIdCardNoVerify([{
            userName: this.data.name.replace(/\./g, '·'),
            idCardNo: this.data.idcard
        }])
        console.log(result, 'result')
        if (result.code && result?.code === 1) {
            // app.toast(result.data[0].msg)
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: result.data[0].msg,
                complete: (res) => {
                    if (res.confirm) {
                        console.log('关闭')
                    }
                }
            })
            return
        }
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.emit('complete', {
            dispatch: this.data.dispatch,
            workType: this.data.workType,
            specialAppoint: this.data.specialAppoint,
            name: this.data.name.replace(/\./g, '·'),
            idcard: this.data.idcard
        })
        // 由上级页面自主返回
        if (!this.data.back) {
            wx.navigateBack(-1)
        }
    },
    async onOcr() {
        const result = await wx.chooseImage({
            sizeType: 'compressed',
            count: 1,
        })
        const base64 = await wxfileTobase64(result.tempFilePaths[0])
        const {
            idCardInfo
        } = await ocrIdCard({
            idCard: base64
        })
        this.setData({
            name: idCardInfo.name,
            idcard: idCardInfo.code
        })
    },
    changeSpecialAppoint() {
        this.data.isselectspecialAppoint = !this.data.isselectspecialAppoint
        this.setData({
            isselectspecialAppoint: this.data.isselectspecialAppoint
        })
    }
})